package com.polaris.server.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.polaris.common.utils.RespBean;
import com.polaris.server.dto.StationQueryParam;
import com.polaris.server.pojo.TrafficStation;
import com.polaris.server.service.ITrafficStationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author polaris
 * @since 2021-06-02
 */
@Api(tags = "station管理")
@RestController
@RequestMapping("/api/traffic/station")
public class TrafficStationController {
    private final ITrafficStationService stationService;

    public TrafficStationController(ITrafficStationService stationService) {
        this.stationService = stationService;
    }

    @ApiOperation(value = "获取数据list")
    @GetMapping("/list")
    public RespBean list(Principal principal, @RequestParam Integer page, @RequestParam Integer limit, @RequestBody StationQueryParam queryParam){
        try{
            Page<TrafficStation> selectPage = new Page<>(page, limit);
            IPage<TrafficStation> list = stationService.list(queryParam, selectPage);
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("total", list.getTotal());
            returnMap.put("data", list.getRecords());
            return RespBean.success("获取站点list成功", returnMap);
        }
        catch (Exception e){
            return RespBean.error("获取站点list失败,"+e.getMessage());
        }
    }

    @ApiOperation(value = "根据索引查询")
    @GetMapping("/get")
    public RespBean get(Principal principal, @RequestBody StationQueryParam queryParam){
        TrafficStation trafficStation = stationService.get(queryParam);
        if(trafficStation!=null){
            return RespBean.success("查询成功",trafficStation);
        }
        else {
            return RespBean.error("查询失败");
        }
    }
}
